package Control;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import Database.*;
import Model.*;

import javax.imageio.ImageIO;
import javax.swing.*;

public class AddUserGUI extends JFrame implements ActionListener
{
	private JTextField jtUser, jtFName, jtLName;
	private JPasswordField password, confirmPassword;
	private JCheckBox isManager;
	private JLabel logoPicture;
	private JButton add, cancel;
	private JPanel logo, p1, p2;
	private TableOperations to;
	private ImageIcon icon = new ImageIcon("frameIcon.png");
	
	public AddUserGUI()
	{
		setIconImage(icon.getImage());
		setTitle("MDL - Add User");
		setSize(425, 350);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);

		GridBagLayout gridbagLayoutSettings = new GridBagLayout();
		GridBagConstraints gc = new GridBagConstraints();
		Insets insets = new Insets(5, 5, 5, 5);
		to = new TableOperations();
		to.queryUsers();

		jtUser = new JTextField(25);
		jtFName = new JTextField(25);
		jtLName = new JTextField(25);
		password = new JPasswordField(25);
		confirmPassword = new JPasswordField(25);
		isManager = new JCheckBox();

		BufferedImage myPicture = null;
		try
		{
			myPicture = ImageIO.read(new File("logo.png"));
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		logo = new JPanel();
		logoPicture = new JLabel(new ImageIcon(myPicture));
		logo.add(logoPicture);
		this.add(logo, BorderLayout.NORTH);

		// Panel 1
		p1 = new JPanel();
		p1.setLayout(gridbagLayoutSettings);

		gc.gridx = 0;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(new JLabel("Username"), gc);

		gc.gridx = 1;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 2;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(jtUser, gc);

		gc.gridx = 0;
		gc.gridy = 2;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(new JLabel("First Name"), gc);

		gc.gridx = 1;
		gc.gridy = 2;
		gc.weighty = 0;
		gc.weightx = 2;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(jtFName, gc);

		gc.gridx = 0;
		gc.gridy = 3;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(new JLabel("Last Name"), gc);

		gc.gridx = 1;
		gc.gridy = 3;
		gc.weighty = 0;
		gc.weightx = 2;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(jtLName, gc);

		gc.gridx = 0;
		gc.gridy = 4;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(new JLabel("Password"), gc);

		gc.gridx = 1;
		gc.gridy = 4;
		gc.weighty = 0;
		gc.weightx = 2;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(password, gc);

		gc.gridx = 0;
		gc.gridy = 5;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(new JLabel("Confirm Password"), gc);

		gc.gridx = 1;
		gc.gridy = 5;
		gc.weighty = 0;
		gc.weightx = 2;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(confirmPassword, gc);

		gc.gridx = 0;
		gc.gridy = 6;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(new JLabel("Manager"), gc);

		gc.gridx = 1;
		gc.gridy = 6;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(isManager, gc);
		isManager.addActionListener(this);

		this.add(p1, BorderLayout.CENTER);

		// Panel 2
		p2 = new JPanel();
		p2.setLayout(gridbagLayoutSettings);

		gc.gridx = 1;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add = new JButton("Add");
		add.addActionListener(this);
		p2.add(add, gc);

		gc.gridx = 3;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		cancel = new JButton("Cancel");
		cancel.addActionListener(this);
		p2.add(cancel, gc);

		this.add(p2, BorderLayout.SOUTH);

	}

	public void addNewUser()
	{
		String username = jtUser.getText();
		String fname = jtFName.getText();
		String lname = jtLName.getText();
		String pw = password.getText();
		String conPw = confirmPassword.getText();
		boolean manager = isManager.isSelected();

		if (username.equals("") || fname.equals("") || lname.equals("") || pw.equals("") || conPw.equals(""))
		{
			JOptionPane.showMessageDialog(null, "Check for Blank Fields");
		} 
		else
		{
			// Checks username
			boolean checkUsername = to.checkUsername(username);
			if (checkUsername == true)
			{
				// Checks password length
				if (pw.length() < 6)
				{
					JOptionPane.showMessageDialog(null,
							"Passwords must be at least 6 characters long");
				} else
				{
					// Checks both password textfields
					if (conPw.equals(pw))
					{
						User u = new User(username, fname, lname, pw);
						to.addUser(u, manager);
						jtUser.setText("");
						jtFName.setText("");
						jtLName.setText("");
						password.setText("");
						confirmPassword.setText("");
						isManager.setSelected(false);
					} else
					{
						JOptionPane.showMessageDialog(null,
								"Passwords do not match");
					}
				}
			} else
			{
				JOptionPane.showMessageDialog(null,
						"Username already been taken. Please re-enter");
			}
		}
	}

	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == add)
		{
			addNewUser();
		} 
		else if (e.getSource() == cancel)
		{
			this.dispose();
		}
	}
}